package com.game;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Timer;
import java.util.TimerTask;

public class MainFrame extends JFrame {
    private Snake snake;
    private JPanel jPanel;
    private Timer timer;
    private Node food;

    public MainFrame(){
        initFrame();
        initPanel();
        initSnake();
        //初始化食物在Timer之前
        initFood();
        initTimer();
        setKeyListener();
    }

    private void initFood() {
        food = new Node();
        food.randomNode();
    }

    //键盘
    private void setKeyListener() {
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                switch (e.getKeyCode()) {
                    case KeyEvent.VK_UP:
                        //不能倒着走
                        if (snake.getDirection()!=Direction.DOWN) {
                            snake.setDirection(Direction.UP);
                        }
                        break;
                    case KeyEvent.VK_DOWN:
                        if (snake.getDirection()!=Direction.UP) {
                            snake.setDirection(Direction.DOWN);
                        }
                        break;
                    case KeyEvent.VK_RIGHT:
                        if (snake.getDirection()!=Direction.LEFT) {
                            snake.setDirection(Direction.RIGHT);
                        }
                        break;
                    case KeyEvent.VK_LEFT:
                        if (snake.getDirection()!=Direction.RIGHT) {
                            snake.setDirection(Direction.LEFT);
                        }
                        break;
                }
            }
        });
    }

    //定时器来控制每次蛇变化
    private void initTimer() {
        //要定时的行为
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                snake.move();
                //判断蛇头与食物
                //先赋值蛇头再判断，否则时间过了head的&&前后就不一样了
                Node head = snake.getBody().getFirst();
                if (head.getX() == food.getX() && head.getY() == food.getY()) {
                    //传参
                    snake.eat();
                    //重置食物
                    initFood();
                }
                //重画
                jPanel.repaint();
            }
        };
        //延迟和每次周期
        timer = new Timer();
        timer.scheduleAtFixedRate(timerTask,0, 100);
    }

    //初始化蛇
    private void initSnake() {
        snake = new Snake();
    }


    //画布
    private void initPanel() {
        jPanel =
                new JPanel(){
            @Override
            public void paint(Graphics g) {
                //每次清空
                g.clearRect(0, 0, 600, 600);
                //画笔黑色
                g.setColor(Color.black);
                //画线
                for (int i = 0; i <= 40; i++) {
                    g.drawLine(0,i*15, 600, i*15);
                }
                for (int i = 0; i <= 40; i++) {
                    g.drawLine(i*15, 0, 15*i, 600);
                }

                //画蛇
                for (Node node : snake.getBody()) {
                    g.fillRect(node.getX()*15, node.getY()*15, 15, 15);
                }

                //画食物
                g.setColor(Color.red);
                g.fillRect(food.getX()*15, food.getY()*15, 15, 15);


            }
        };
        add(jPanel);

    }

    private void initFrame() {
        setTitle("贪吃蛇");
        setSize(618, 640);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
    }

    /*public static void main(String[] args) {
        MainFrame frame = new MainFrame();
        frame.setVisible(true);
    }*/

}
